package com.controller;

import com.entity.RbacManager;
import com.entity.ResponseEntity;
import com.service.RbacManagerService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 对rbacManager表进行增删改查的controller层
 *
 * @author liuyukai
 * @date 2022/04/17
 */
@RestController
@CrossOrigin
@RequestMapping("/rbacManager")
public class RbacManagerController {

    @Autowired
    private RbacManagerService rbacManagerService;

    /**
     * 将数据插入到数据库中
     *
     * @param rbacManager 封装的实体类
     * @return {@link ResponseEntity} 封装好的统一响应实体
     */
    @PostMapping("/User")
    public ResponseEntity add(@RequestBody RbacManager rbacManager){
        System.out.println("rbacManager = " + rbacManager);
        return rbacManagerService.addOne(rbacManager);
    }

    /**
     * 根据前端传入的id删除数据库对应数据
     *
     * @param id 需要删除的数据id
     * @return {@link ResponseEntity} 封装好的统一响应实体
     */
    @DeleteMapping("/User/{id}")
    public ResponseEntity delete(@PathVariable int id){
        return rbacManagerService.deleteOne(id);
    }


    /**
     * 更新用户方法
     *
     * @param rbacManager 封装的实体类
     * @return {@link ResponseEntity} 封装好的统一响应实体
     */
    @PutMapping(value = "/User")
    public ResponseEntity updateOne(@RequestBody RbacManager rbacManager){
        System.out.println("rbacManager = " + rbacManager);
        return rbacManagerService.updateOne(rbacManager);
    }

    /**
     * 统一的查询方法，包含统一查询模糊查询和单个查询
     * 根据封装类中的标志位传递给下层判断进行何种查询
     *
     * @param startPage   开始页面
     * @param pageSize    页面大小
     * @param rbacManager 封装的实体类
     * @return {@link ResponseEntity} 封装好的统一响应实体
     */
    @PostMapping("/User/{startPage}/{pageSize}")
    public ResponseEntity find(@PathVariable int startPage,@PathVariable int pageSize,@RequestBody RbacManager rbacManager){
        System.out.println("rbacManager = " + rbacManager);
        ResponseEntity all = rbacManagerService.find(startPage,pageSize,rbacManager);
        PageInfo info = new PageInfo<RbacManager>((List<RbacManager>) all.getData());
        all.setData(info);
        return all;
    }
}
